#pragma once
/**
 * @file HttpRequest.hpp
 * @author 1102403684@qq.com
 * @brief 封装Http请求
 * @version 0.1
 * @date 2024-10-28
 * 
 * @copyright Copyright (c) 2024
 * 
 */

#include <string>
#include <unordered_map>
#include <regex>

class HttpRequest
{
public:
    HttpRequest(){

    }


public:
    std::string method_;
    std::string path_;
    std::string version_;
    std::unordered_map<std::string, std::string> headers_;
    std::unordered_map<std::string, std::string> params_;

    std::string body_;
    std::smatch match_;
public:

    void set_header(const std::string& key, const std::string& val)
    {
        headers_.insert(std::make_pair(key,val));
    }
    std::string get_header(const std::string& key) const
    {
        if(!has_header(key)) return "";

        return headers_.at(key);
    }
    bool has_header(const std::string& key) const
    {
        auto it = headers_.find(key);
        if(it == headers_.end()){
            return false;
        }
        return true;
    }

    void set_param(const std::string& key, const std::string& val)
    {
        headers_.insert(std::make_pair(key,val));
    }
    std::string get_param(const std::string& key)
    {
        if(!has_param(key)) return "";
        return params_[key];
    }
    bool has_param(const std::string& key)
    {
        auto it = params_.find(key);
        if(it == params_.end()){
            return false;
        }
        return true;
    }

    size_t get_content_length()
    {
        if(!has_header("Content-Length")){
            return 0;
        }

        std::string content_length = get_header("Content-Length");
        return std::stol(content_length);
    }

    //是否是长连接
    bool is_keep_alive() const
    {
        if(has_header("Connection") && get_header("Connection") == "keep-alive"){
            return true;
        }
        return false;
    }



    void set_method(const std::string& method){method_ = method;}
    std::string get_method() const {return method_;}

    void set_path(const std::string& path){path_ = path;}
    std::string get_path() const {return path_;}

    void set_version(const std::string& version){version_ = version;}
    std::string get_version() const {return version_;}
    
    void set_body(const std::string& body){body_ = body;}
    std::string get_body() const {return body_;}

    //TODO append支持给定一个地址和size来追加，后面可以进行修改
    void append_body(const char* body, size_t len){
        body_.append(body, len);
    }


    void clear()
    {
        method_.clear();
        path_.clear();
        version_.clear();
        body_.clear();
        std::smatch match;
        match_.swap(match);
        headers_.clear();
        params_.clear();
    }
};